﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AutoCadConvert
{
    public class LoginInfo
    {
        public string token = "";
        public string userNameBase64 = "";
        public string userName = "";
        public string passwordMD5 = "";
        public bool isLogin = false;
        public string globalfileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "swjx_data.dat");
        public string dwgFileDirectory = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "siweijuxiang");

        private static LoginInfo m_inst;
        public static LoginInfo instance()
        {
            if (m_inst == null)
                m_inst = new LoginInfo();
            return m_inst;
        }
        public bool ParseFile()
        {
            //自动登陆
            try
            {
                if (File.Exists(LoginInfo.instance().globalfileName))
                {
                    string fileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "swjx_data.dat");
                    string content = File.ReadAllText(fileName);
                    string[] data = content.Split(new char[] { '\n' });
                    if (data.Count() < 3) return false;
                    LoginInfo.instance().userName = data[0];
                    LoginInfo.instance().userNameBase64 = data[1];
                    LoginInfo.instance().passwordMD5 = data[2];
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                DwgConvert.PutMessage(ex.Message);
                return false;
            }
        }
        public bool AutoLogin()
        {
            if (ParseFile())
            {
                HttpManager.UserLogin_Response ret = HttpManager.AutoLogin(userNameBase64, passwordMD5);
                if (ret != null && ret.code == 1001)
                {
                    isLogin = true;
                    if (string.IsNullOrEmpty(userName))
                        return false;
                    token = ret.data.token;
                    return true;
                }
            }

            return false;
        }
        public bool Login(string username,string password,ref string errMsg)
        {
            HttpManager.UserLogin_Response ret = HttpManager.UserLogin(username, password);
            if (ret!=null)
            {
                if (ret.code == 1001)
                {
                    isLogin = true;
                    userName = username;
                    token = ret.data.token;
                    errMsg = "";
                    return true;
                }
                else
                {
                    errMsg = ret.msg;
                    return false;
                }
            }
            else
            {
                errMsg = "服务器连接异常";
                return false;
            }
        }
        public bool Logout(string username)
        {
            return true;
        }

        public string AppPath
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
            }
        }
    }
}
